package com.clj.blesample.menu;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.clj.blesample.BaseActivity;
import com.clj.blesample.compose.BaseEmptyTransformer;
import com.clj.blesample.databinding.ActivityErrorUploadBinding;
import com.clj.blesample.entity.AppConstant;
import com.clj.blesample.entity.Result;
import com.clj.blesample.http.exception.RequestFailedException;
import com.clj.blesample.http.retrofit.RetrofitManager;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;

public class ErrorUploadActivity extends BaseActivity {
    private ActivityErrorUploadBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityErrorUploadBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        setSupportActionBar(mBinding.toolbar);
        getSupportActionBar().setTitle("异常数据上传");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        mBinding.inputDate.setContent(format.format(System.currentTimeMillis()));
        mBinding.inputCollectNo.getEtContent().setInputType(InputType.TYPE_CLASS_NUMBER);
//        if(BuildConfig.DEBUG){
//            mBinding.inputProjectNo.setContent("RO-TEST-230518");
//            mBinding.inputProjectName.setContent("测试");
//            mBinding.inputCollectNo.setContent("1");
//            mBinding.inputErrorDesc.setContent("test");
//        }
        mBinding.tvUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String projectNo = mBinding.inputProjectNo.getContent();
                String projectName = mBinding.inputProjectName.getContent();
                String collectNo = mBinding.inputCollectNo.getContent();
                String errorDesc = mBinding.inputErrorDesc.getContent();
                if (TextUtils.isEmpty(projectNo)) {
                    Toast.makeText(ErrorUploadActivity.this, "项目编号必填", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(projectName)) {
                    Toast.makeText(ErrorUploadActivity.this, "项目名称必填", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(collectNo)) {
                    Toast.makeText(ErrorUploadActivity.this, "焊口编号必填", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(errorDesc)) {
                    Toast.makeText(ErrorUploadActivity.this, "异常描述必填", Toast.LENGTH_SHORT).show();
                    return;
                }
                showProgress();
                Map<String, Object> params = new HashMap<>();
                params.put("F_MarkingTime", mBinding.inputDate.getContent());
                params.put("F_ProjectCode", projectNo);
                params.put("F_ProjectName", projectName);
                params.put("F_WeldNum", collectNo);
                params.put("F_Latitude", AppConstant.mLatitude);
                params.put("F_Longitude", AppConstant.mLongitude);
                params.put("F_Description", errorDesc);
                RetrofitManager.getApi().updateAnomaly(params)
                        .compose(new BaseEmptyTransformer<>())
                        .subscribe(new DisposableObserver<Result<Object>>() {
                            @Override
                            public void onNext(Result<Object> objectResult) {
                                Toast.makeText(ErrorUploadActivity.this, "异常数据上传成功！", Toast.LENGTH_SHORT).show();
                                finish();
                            }

                            @Override
                            public void onError(Throwable e) {
                                e.printStackTrace();
                                if (e instanceof RequestFailedException) {
                                    RequestFailedException exception = (RequestFailedException) e;
                                    if (exception.result != null && !TextUtils.isEmpty(exception.result.msg)) {
                                        Toast.makeText(ErrorUploadActivity.this, exception.result.msg, Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }

                            @Override
                            public void onComplete() {
                                hideProgress();
                            }
                        });
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
